<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="qldc.pm.csdl.cd90.service.CongDanLocalServiceUtil"%>
<%@page import="qldc.pm.qtht.dmdc.service.QuanHuyenLocalServiceUtil"%>
<%@page import="qldc.pm.qtht.dmdc.model.QuanHuyen"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portal.kernel.util.Constants"%>
<%@page import="qldc.pm.qtht.dmdc.utils.DiaChiUtils"%>
<%@page import="qldc.pm.csdl.cd90.service.CongDanDTOLocalServiceUtil"%>
<%@page import="qldc.pm.tiensu.searchcontainer.CongDanSearchTerms"%>
<%@page import="qldc.pm.tiensu.searchcontainer.CongDanDisplayTerms"%>
<%@page import="qldc.pm.tiensu.searchcontainer.CongDanSearch"%>
<%@page import="qldc.pm.csdl.cd90.model.dto.CongDanDTO"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="qldc.pm.qtht.dmdc.service.TinhLocalServiceUtil"%>
<%@page import="qldc.pm.qtht.dmdc.model.Tinh"%>
<%@page import="java.util.List"%>

<%@include file="../../init.jsp"%>

<%
PortletURL portletURL = renderResponse.createRenderURL();
portletURL.setParameter("mvcPath", "/html/view.jsp");
portletURL.setParameter("chonChucNangItem", TienSuConstants.QUAN_LY_NGUOI_CO_TIEN_SU);
portletURL.setParameter("tabs1", "cap_nhat_nguoi_co_tien_su");

String redirect = ParamUtil.getString(request, "redirect");

List<Tinh> tinhsMacDinh = TinhLocalServiceUtil.findByLaMacDinh(true);

Tinh tinhMacDinh = TinhLocalServiceUtil.findTinhMacDinh();

List<QuanHuyen> quanHuyens = QuanHuyenLocalServiceUtil.findByTinhId(tinhMacDinh.getTinhId());

PortletURL quanLyNguoiCoTienSuURL = renderResponse.createRenderURL();
quanLyNguoiCoTienSuURL.setParameter("mvcPath", "/html/view.jsp");
quanLyNguoiCoTienSuURL.setParameter("chonChucNangItem", TienSuConstants.QUAN_LY_NGUOI_CO_TIEN_SU);
%>

<portlet:resourceURL var="loadPhuongXa" id="loadPhuongXa">
	<portlet:param name="quanHuyenIdSelected" value="{0}" />
</portlet:resourceURL>

<script>
function <portlet:namespace/>searchValidate(){
	var soCMND = $('#<portlet:namespace/>soCMND');
	if(soCMND.val() != '' && !validateIntegerWithLength(soCMND.val(), 1, 12)) {
		showPopupConfirm('<liferay-ui:message key="so-cmnd-nay-khong-hop-le" />',['Ok'],function(index){
			soCMND.focus();
		});
		return false;
	} else {
		var form = $("#<portlet:namespace/>fm");
		form.attr('onsubmit', 'return true;')
		form.submit();
	}
}
</script>

<form action="<%=portletURL.toString() %>" method="POST" name="<portlet:namespace/>fm" id="<portlet:namespace/>fm" onsubmit="return false;">
	<%
		CongDanSearch congDanSearch = new CongDanSearch(renderRequest, portletURL);
		CongDanDisplayTerms congDanDisplayTerms = (CongDanDisplayTerms) congDanSearch.getDisplayTerms();
		CongDanSearchTerms congDanSearchTerms = (CongDanSearchTerms) congDanSearch.getSearchTerms();
	%>
	<table class="gridnormal">
		<tr>
			<td class="aui-w15"><liferay-ui:message key="so-dinh-danh-ca-nhan" /></td>
			<td class="aui-w35"><input type="text" name="<portlet:namespace/>soDinhDanhCaNhan" value="<%=congDanDisplayTerms.getSoDinhDanhCaNhan() %>" /></td>
			<td class="aui-w15"><liferay-ui:message key="so-cmnd" /></td>
			<td class="aui-w35"><input type="text" name="<portlet:namespace/>soCMND" id="<portlet:namespace/>soCMND" value="<%=congDanDisplayTerms.getSoCMND() %>" /></td>
		</tr>

		<tr>
			<td><liferay-ui:message key="ho-va-ten" /></td>
			<td><input type="text" name="<portlet:namespace/>hoVaTen" id="<portlet:namespace/>hoVaTen" value="<%=congDanDisplayTerms.getHoVaTen() %>" /></td>
			<td><liferay-ui:message key="so-so-ho-khau" /></td>
			<td><input type="text" name="<portlet:namespace/>soSoHoKhau" value="<%=congDanDisplayTerms.getSoSoHoKhau() %>" /></td>
		</tr>

		<tr>
			<td><liferay-ui:message key="noi-thuong-tru" /></td>

			<td colspan="2">
				<select class="display-inline" style="width: 30%;" disabled="disabled">
					<option value="<%=tinhMacDinh.getTinhId() %>" selected="selected"><%=tinhMacDinh.getTenTinh() %></option>
				</select>
				
				<select class="display-inline" style="width: 34%" name="<portlet:namespace/>noiThuongTruQuanHuyenId" id="<portlet:namespace/>noiThuongTruQuanHuyenId" onchange="loadPhuongXa('<portlet:namespace/>noiThuongTruQuanHuyenId', '<portlet:namespace/>noiThuongTruPhuongXaId', '<%=loadPhuongXa.toString() %>', '<%=congDanDisplayTerms.getNoiThuongTruPhuongXaId() %>');">
					<option value="0"><liferay-ui:message key="chon-quan-huyen" /></option>
				
					<%
					for(QuanHuyen quanHuyen : quanHuyens) {
					%>
					<option value="<%=quanHuyen.getQuanHuyenId() %>" <%=congDanDisplayTerms.getNoiThuongTruQuanHuyenId() == quanHuyen.getQuanHuyenId() ? "selected=selected" : "" %>><%=quanHuyen.getTenQuanHuyen() %></option>
					<%
					}
					%>
				</select>
				
				<select class="display-inline" style="width: 34%" name="<portlet:namespace/>noiThuongTruPhuongXaId" id="<portlet:namespace/>noiThuongTruPhuongXaId">
					<option value="0"><liferay-ui:message key="chon-phuong-xa" /></option>
				</select>
			</td>
			<td><input type="text" name="<portlet:namespace/>noiThuongTruDiaChi" id="<portlet:namespace/>noiThuongTruDiaChi" value="<%=congDanDisplayTerms.getNoiThuongTruDiaChi()%>" /></td>
		</tr>

		<tr>
			<td></td>
			<td colspan="3">
				<div align="left">
					<input type="submit" onclick="<portlet:namespace/>searchValidate();" class="btn btn-primary" value="<liferay-ui:message key="tim-cong-dan" />" >
					<input type="button" class="btn" onclick="javascript:submitForm(document.hrefFm,'<%=quanLyNguoiCoTienSuURL.toString() %>');" value="<liferay-ui:message key="tro-ve" />" />
				</div>
			</td>
		</tr>
	</table>
</form>

<liferay-ui:search-container searchContainer="<%=congDanSearch %>">
	<liferay-ui:search-container-results>
	<%
		if(Validator.isNull(congDanSearchTerms.getSoDinhDanhCaNhan())
				&& Validator.isNull(congDanSearchTerms.getSoCMND())
				&& Validator.isNull(congDanSearchTerms.getHoVaTen())
				&& Validator.isNull(congDanSearchTerms.getSoSoHoKhau())
				&& (congDanSearchTerms.getNoiThuongTruQuanHuyenId() == 0L)
				&& (congDanSearchTerms.getNoiThuongTruPhuongXaId() == 0L)
				&& Validator.isNull(congDanSearchTerms.getNoiThuongTruDiaChi())) {
			results = null;
			total = 0;
		} else {
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put(CongDanDTOLocalServiceUtil.SO_DINH_DANH_CA_NHAN, congDanSearchTerms.getSoDinhDanhCaNhan());
			parameters.put(CongDanDTOLocalServiceUtil.SO_CMND, congDanSearchTerms.getSoCMND());
			parameters.put(CongDanDTOLocalServiceUtil.HO_VA_TEN, congDanSearchTerms.getHoVaTen());
			parameters.put(CongDanDTOLocalServiceUtil.SO_SO_HO_KHAU, congDanSearchTerms.getSoSoHoKhau());
			parameters.put(CongDanDTOLocalServiceUtil.NOI_THUONG_TRU_TINH_ID, tinhMacDinh.getTinhId());
			parameters.put(CongDanDTOLocalServiceUtil.NOI_THUONG_TRU_QUAN_HUYEN_ID, congDanSearchTerms.getNoiThuongTruQuanHuyenId());
			parameters.put(CongDanDTOLocalServiceUtil.NOI_THUONG_TRU_PHUONG_XA_ID, congDanSearchTerms.getNoiThuongTruPhuongXaId());
			parameters.put(CongDanDTOLocalServiceUtil.NOI_THUONG_TRU_DIA_CHI, congDanSearchTerms.getNoiThuongTruDiaChi());
			 
			results = CongDanDTOLocalServiceUtil.findCongDansAndAllAttributesByParameters(parameters, congDanSearch.getStart(), congDanSearch.getEnd(), congDanSearch.getOrderByComparator());
			total = CongDanDTOLocalServiceUtil.countByParameters(parameters);
		}

		pageContext.setAttribute("results", results);
		pageContext.setAttribute("total", total);
	%>
	</liferay-ui:search-container-results>
	<%
		int start = congDanSearch.getDelta() * (congDanSearch.getCur() - 1);
	%>
	<liferay-ui:search-container-row className="qldc.pm.csdl.cd90.model.dto.CongDanDTO" keyProperty="congDanId" modelVar="congDan">
		<%
			PortletURL themTienSu = renderResponse.createRenderURL();
			themTienSu.setParameter("mvcPath", "/html/view.jsp");
			themTienSu.setParameter("chonChucNangItem", TienSuConstants.QUAN_LY_NGUOI_CO_TIEN_SU);
			themTienSu.setParameter("tabs1", "cap_nhat_tien_su");
			themTienSu.setParameter("soDinhDanhCaNhan", congDan.getSoDinhDanhCaNhan());
			themTienSu.setParameter("redirect", portletURL.toString());
			themTienSu.setParameter(Constants.CMD, Constants.ADD);
			
			long noiThuongTruQuanHuyenId = congDan.getNoiThuongTruQuanHuyenId();
			long noiThuongTruPhuongXaId = congDan.getNoiThuongTruPhuongXaId();
			String noiThuongTruDiaChi = congDan.getNoiThuongTruDiaChi();
			
			String noiThuongTru = DiaChiUtils.createBlockDiaChi(noiThuongTruQuanHuyenId, noiThuongTruPhuongXaId, noiThuongTruDiaChi);
		%>
		
		<liferay-ui:search-container-column-text cssClass="txt-center" name='stt' value="<%=String.valueOf(++start)%>" />
		
		<liferay-ui:search-container-column-text href="<%=themTienSu.toString() %>" name='ho-va-ten' property="hoVaTen" orderable="<%=true %>" orderableProperty="hoVaTen" />
		
		<liferay-ui:search-container-column-text href="<%=themTienSu.toString() %>" cssClass="txt-center" name='so-dinh-danh-ca-nhan' property="soDinhDanhCaNhan" orderable="<%=true %>" orderableProperty="soDinhDanhCaNhan" />
		
		<liferay-ui:search-container-column-text href="<%=themTienSu.toString() %>" cssClass="txt-center" name='so-cmnd' property="soCMND" orderable="<%=true %>" orderableProperty="soCMND" />
		
		<liferay-ui:search-container-column-text href="<%=themTienSu.toString() %>" name='so-so-ho-khau' property="soSoHoKhau" orderable="<%=true %>" orderableProperty="soSoHoKhau" />
		
		<liferay-ui:search-container-column-text name='noi-thuong-tru' value="<%=noiThuongTru %>" />
		
		<liferay-ui:search-container-column-text align="center">
			<liferay-ui:icon image="recent_changes" message="them-tien-su" url="<%=themTienSu.toString() %>" />
		</liferay-ui:search-container-column-text>
	</liferay-ui:search-container-row>
	<liferay-ui:search-iterator searchContainer="<%=congDanSearch %>" />

</liferay-ui:search-container>

<script type="text/javascript">
$(function() {
	loadPhuongXa('<portlet:namespace/>noiThuongTruQuanHuyenId', '<portlet:namespace/>noiThuongTruPhuongXaId', '<%=loadPhuongXa.toString() %>', '<%=congDanDisplayTerms.getNoiThuongTruPhuongXaId() %>');
});
</script>